package com.daiqile.myinterest.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.daiqile.myinterest.R;
import com.daiqile.myinterest.overlayutil.PoiOverlay;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class POIActivity extends AppCompatActivity implements OnGetPoiSearchResultListener {
    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.et_city)
    AppCompatEditText etCity;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.et_addr)
    AppCompatEditText etAddr;
    @BindView(R.id.et_key)
    AppCompatEditText etKey;

    private PoiSearch mPoiSearch = null;
    private BaiduMap mBaiduMap;
    LatLng center = new LatLng(29.814112, 121.554961);
    int radius =1000;
    int type = 0 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi);
        ButterKnife.bind(this);
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);
        mBaiduMap = mapView.getMap();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPoiSearch.destroy();
        mapView.onDestroy();
        mBaiduMap = null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @OnClick({R.id.btn_go, R.id.btn_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_go:
                String city = etCity.getText().toString();
                String addr = etAddr.getText().toString();
                mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .city(city)
                        .keyword(addr)
                        .pageNum(0));
                break;
            case R.id.btn_search:
                type = 2 ;
                String key = etKey.getText().toString();
                PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(key).sortType(PoiSortType.distance_from_near_to_far).location(center)
                        .radius(radius).pageNum(1);
                mPoiSearch.searchNearby(nearbySearchOption);
                break;
        }
    }

    private class MyPoiOverlay extends PoiOverlay {

        public MyPoiOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int index) {
            super.onPoiClick(index);
            PoiInfo poi = getPoiResult().getAllPoi().get(index);
            // if (poi.hasCaterDetails) {
            mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
                    .poiUid(poi.uid));
            // }
            return true;
        }
    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            Toast.makeText(POIActivity.this, "未找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            mBaiduMap.clear();
            mBaiduMap.hideInfoWindow();
            PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(result);
            overlay.addToMap();
            overlay.zoomToSpan();
            switch(type) {
                case 2:
                    showNearbyArea(center, radius);
                    break;
                default:
                    break;
            }
            return;
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
            // 当输入关键字在本市没有找到，但在其他城市找到时，返回包含该关键字信息的城市列表
            String strInfo = "在";
            for (CityInfo cityInfo : result.getSuggestCityList()) {
                strInfo += cityInfo.city;
                strInfo += ",";
            }
            strInfo += "找到结果";
            Toast.makeText(POIActivity.this, strInfo, Toast.LENGTH_LONG)
                    .show();
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult result) {
        if (result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(POIActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(POIActivity.this, result.getName() + ": " + result.getAddress(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }

    public void showNearbyArea( LatLng center, int radius) {
        BitmapDescriptor centerBitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_mapmark);
        MarkerOptions ooMarker = new MarkerOptions().position(center).icon(centerBitmap);
        mBaiduMap.addOverlay(ooMarker);
        OverlayOptions ooCircle = new CircleOptions().fillColor(Color.argb(100, 135,206,235))
                .center(center).stroke(new Stroke(5, Color.argb(255, 135,206,235)))
                .radius(radius);
        mBaiduMap.addOverlay(ooCircle);
                //创建InfoWindow展示的view
        View infoView = View.inflate(getApplicationContext(), R.layout.item_infowindow, null);
        Button button = new Button(getApplicationContext());
        button.setText("瓜皮");
        button.setTextColor(Color.argb(255, 255,255,255));
                //创建InfoWindow , 传入 view， 地理坐标， y 轴偏移量
        final InfoWindow mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(infoView), center, -100, new InfoWindow.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick() {
                //点击InfoWindow隐藏
//                mBaiduMap.hideInfoWindow();
            }
        });
        //显示InfoWindow
        mBaiduMap.showInfoWindow(mInfoWindow);
        type = 0;
    }
}
